//
//  CourseClassItemCell.m
//  ShowYork
//
//  Created by hxh on 2020/8/28.
//  Copyright © 2020 hxh. All rights reserved.
//

#import "LessonClassItemCell.h"

@interface LessonClassItemCell ()
@property (nonatomic,strong)UIView *shadowView;
@property (nonatomic,strong)UIView *containView;
@property (nonatomic,strong)UIImageView *coverImgView;
@property (nonatomic,strong)UIImageView *iconImgView;
@property (nonatomic,strong)UILabel *contentLb;
@property (nonatomic,strong)UIImageView *headerImgView;
@property (nonatomic,strong)UILabel *nameLb;
@property (nonatomic,strong)UIButton *confirmBtn;
@property (nonatomic,strong)UIButton *coverBtn;

@end

@implementation LessonClassItemCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUI];
    }
    return self;
}
- (void)setUI{
    
    
    self.shadowView             = [UIView new];
    [self.contentView addSubview:self.shadowView];    
    self.shadowView.layer.shadowColor   = C(240, 1).CGColor;
    self.shadowView.layer.shadowOffset  = CGSizeMake(0, 2);
    self.shadowView.layer.shadowOpacity = 0.8;
    self.shadowView.layer.shadowRadius  = 4;
    
    
    self.containView = [[UIView alloc]init];
    self.containView.layer.cornerRadius = 5;
    self.containView.layer.masksToBounds = YES;
    self.containView.layer.backgroundColor = [UIColor whiteColor].CGColor;
    [self.shadowView addSubview:self.containView];
    
    
    [self.shadowView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.equalTo(self.containView);
    }];
    [self.containView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView);
        make.right.equalTo(self.contentView);
         make.top.bottom.equalTo(self.contentView);
    }];
    
    // 设置宽高比
    CGFloat rate = 92.0/165.0f;
    CGFloat itemH = (kScreenW - 48)/2 * rate;
    
    self.coverImgView = [[UIImageView alloc]init];
    self.coverImgView.backgroundColor = RGBA(221, 221, 221, 1);
    [self.containView addSubview:self.coverImgView];
    [self.coverImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.containView);
        make.height.mas_equalTo(itemH);
    }];
    
    self.iconImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"course_tag_chinese"]];
    
    [self.containView addSubview:self.iconImgView];
    [self.iconImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.coverImgView.mas_bottom).offset(11);
        make.left.equalTo(self.coverImgView).offset(4);
    }];
    
    self.contentLb = [[UILabel alloc]init];
    self.contentLb.font = font(14);
    self.contentLb.textColor = RGBA(51, 51, 51, 1);
    self.contentLb.numberOfLines = 2;
        
//    self.contentLb.lineBreakMode = NSLineBreakByTruncatingTail;
    
    [self.containView addSubview:self.contentLb];
       
    [self.contentLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self.iconImgView);
        make.right.equalTo(self.coverImgView).offset(-4);
    }];

    
    self.headerImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"defaut_header"]];
    self.headerImgView.layer.cornerRadius = 10;
    self.headerImgView.layer.masksToBounds = YES;
    [self.containView addSubview:self.headerImgView];
    [self.headerImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(20, 20));
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-16);
        make.left.equalTo(self.iconImgView);
    }];
    
    self.nameLb = [[UILabel alloc]init];
    self.nameLb.textColor = RGBA(153, 153, 153, 1);
    self.nameLb.font = font(12);
    [self.containView addSubview:self.nameLb];
    [self.nameLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.headerImgView.mas_right).offset(8);
        make.bottom.equalTo(self.headerImgView);
    }];
    
    self.coverBtn = [[UIButton alloc]init];
    [self.coverBtn addTarget:self action:@selector(coverMethod) forControlEvents:UIControlEventTouchUpInside];
    [self.containView addSubview:self.coverBtn];
    [self.coverBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.contentView);
    }];

    
    self.confirmBtn = [[UIButton alloc]init];
    [self.confirmBtn setTitle:@"+加入学习" forState:UIControlStateNormal];
    [self.confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.confirmBtn.titleLabel.font = font(12);
    self.confirmBtn.layer.cornerRadius = 13;
    self.confirmBtn.layer.borderWidth = 0.5;
    self.confirmBtn.layer.masksToBounds = YES;
    [self.confirmBtn addTarget:self action:@selector(confirmMethod) forControlEvents:UIControlEventTouchUpInside];
    [self.containView addSubview:self.confirmBtn];
    
    [self.confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(80, 26));
        make.right.equalTo(self.containView).offset(-8);
        make.bottom.equalTo(self.contentView).offset(-10);
    }];
    
}
- (void)setModel:(LessonPageItemModel *)model{
    
    
    NSArray *ids = [RPAppManager getOwnerPlanIds];
    
    model.isSeleted = [ids containsObject:model.id];
    
    _model = model;
    
    NSMutableParagraphStyle*style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
    //对齐方式
    style.alignment = NSTextAlignmentLeft;
    style.headIndent = 0.0f;//行首缩进进
    style.tailIndent = 0.0f;//行尾缩进
    style.lineBreakMode = NSLineBreakByTruncatingTail;
    //首行缩进
    style.firstLineHeadIndent= self.iconImgView.image.size.width;
    NSAttributedString*attrText = [[NSAttributedString alloc] initWithString:model.title attributes:@{NSParagraphStyleAttributeName: style}];
    self.contentLb.attributedText = attrText;

    self.nameLb.text = model.teacherName;
//    self.contentLb.text = model.title;
    if (model.head.length == 0) {
        self.headerImgView.hidden = YES;
        self.nameLb.hidden = YES;
    }else{
        self.headerImgView.hidden = NO;
        self.nameLb.hidden = NO;
        [self.headerImgView sd_setImageWithURL:[NSURL URLWithString:model.head] placeholderImage:[UIImage imageNamed:@"defaut_header"]];
    }
    
    [self.coverImgView sd_setImageWithURL:[NSURL URLWithString:model.cover]];
    
    if (model.subResultId.intValue == 1) {
        self.iconImgView.image = [UIImage imageNamed:@"course_tag_chinese"];
    }
    if (model.subResultId.intValue == 2) {
        self.iconImgView.image = [UIImage imageNamed:@"course_tag_math"];
        
    }
    if (model.subResultId.intValue == 3) {
        self.iconImgView.image = [UIImage imageNamed:@"course_tag_english"];
    }
    self.confirmBtn.userInteractionEnabled = !model.isSeleted;
    [self.confirmBtn setTitleColor:model.isSeleted?RGBA(153, 153, 153, 1):[RPAppManager themColor] forState:UIControlStateNormal];
    self.confirmBtn.layer.borderColor = model.isSeleted?RGBA(153, 153, 153, 1).CGColor:[RPAppManager themColor].CGColor;
    [self.confirmBtn setTitle:model.isSeleted?@"已加入学习":@"+加入学习" forState:UIControlStateNormal];
}
- (void)confirmMethod{
    if (self.delegate && [self.delegate respondsToSelector:@selector(addCoursePlanAtCurrentModel:)]) {
        [self.delegate addCoursePlanAtCurrentModel:self.model];
    }
}
- (void)coverMethod{
    if (self.delegate && [self.delegate respondsToSelector:@selector(showCourseDetailWithCurrentModel:)]) {
        [self.delegate showCourseDetailWithCurrentModel:self.model];
    }
}

@end
